package com.fjrcloud.controller;

import com.fjrcloud.entity.HouseholdMaster;
import com.fjrcloud.entity.ProjectGroup;
import com.fjrcloud.entity.ResponseBean;
import com.fjrcloud.entity.ResponseFactory;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.service.HouseholdService;
import com.fjrcloud.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shengjian.lin on 17-1-7.
 */
@Controller
@RequestMapping("api/household")
public class HouseholdController {

    @Autowired
    private HouseholdService service;

    @RequestMapping("list/master")
    @ResponseBody
    public ResponseBean listMaster() throws BusinessException {
        List<HouseholdMaster> list = service.masters(null,null).getContent();
//        Map<String,Map<String,Object>> result = new HashMap<String, Map<String,Object>>();
//        for (HouseholdMaster master: list) {
//            Map<String,Object> map = result.get(master.getIdNo());
//
//            if (map == null) {
//                map = new HashMap<String, Object>();
//                ProjectGroup group = master.getGroup();
//                group.setUsers(null);
//                map.put("group",group);
//                map.put("house",new ArrayList<HouseholdMaster>());
//
//                result.put(master.getIdNo(),map);
//            }
//
//            ((List)map.get("house")).add(master);
//
//        }
//        return ResponseFactory.getResponseBean(StatusUtils.Success,result.values());

        Map<String,Map<String,Object>> result = new HashMap<String, Map<String,Object>>();
        for (HouseholdMaster master: list) {
            Map<String,Object> map = result.get(master.getId());

            if (map == null) {
                map = new HashMap<String, Object>();
                ProjectGroup group = master.getGroup();
                group.setUsers(null);
                map.put("group",group);

                result.put(master.getIdNo(),map);
            }
            map.put("house",master);

        }
        return ResponseFactory.getResponseBean(StatusUtils.Success,result.values());
    }
    @RequestMapping("set/disposable")
    @ResponseBody
    public ResponseBean disposable(Integer masterId,Double price) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.setDisposable(masterId,price));
    }
    @RequestMapping("set/buyArea")
    @ResponseBody
    public ResponseBean buyArea(Integer masterId,Double buyArea) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.setBuyArea(masterId,buyArea));
    }
    @RequestMapping("set/errorArea")
    @ResponseBody
    public ResponseBean errorArea(Integer masterId,Double errorArea) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.setErrorArea(masterId,errorArea));
    }
    @RequestMapping("save/master")
    @ResponseBody
    public ResponseBean saveMaster(HouseholdMaster master) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.saveMaster(master));
    }
//    @RequestMapping("save/archive")
//    @ResponseBody
//    public ResponseBean saveArchive(Integer master,Integer template,String data) throws BusinessException {
//        return ResponseFactory.getResponseBean(StatusUtils.Success,service.saveArchive(master,template,data));
//    }
    @RequestMapping("get/archive")
    @ResponseBody
    public ResponseBean getArchive(Integer id) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.getArchive(id));
    }
    @RequestMapping("get/master")
    @ResponseBody
    public ResponseBean getMaster(Integer id) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.getMaster(id));
    }
    @RequestMapping("del/master")
    @ResponseBody
    public ResponseBean delMaster(Integer id) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.delMaster(id));
    }
//    @RequestMapping("archive/refresh")
//    @ResponseBody
//    public ResponseBean archiveRefresh(Integer id) throws BusinessException {
//        return ResponseFactory.getResponseBean(StatusUtils.Success,service.archiveRefresh(id));
//    }
    @RequestMapping("save/collection")
    @ResponseBody
    public ResponseBean saveCollection(Integer id,String name) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.saveCollection(id,name));
    }
    @RequestMapping("upload")
    @ResponseBody
    public ResponseBean upload(Integer id,MultipartFile file,String name) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.upload(id,file,name));
    }
    @RequestMapping("get/files")
    @ResponseBody
    public ResponseBean getFiles(Integer id) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.getFiles(id));
    }
//    @RequestMapping("print")
//    @ResponseBody
//    public ResponseBean print(Integer[] masterIds, Integer archiveId,Boolean current) throws BusinessException {
//        return ResponseFactory.getResponseBean(StatusUtils.Success,service.print(masterIds,archiveId,current,null,null));
//    }

    @RequestMapping("find")
    @ResponseBody
    public ResponseBean find(HouseholdMaster master) throws BusinessException {
        if (master.getId() == null)
            return ResponseFactory.getResponseBean(StatusUtils.Success,service.find(master,null,null));
        else
            return ResponseFactory.getResponseBean(StatusUtils.Success,service.getMaster(master.getId()));
    }
}
